/**
 *
 */
package com.engine.Mangers;

import java.util.List;

import org.hibernate.Session;

import com.util.EngineUtil;
import com.wfe.db.SessionUtil;
import com.wfe.db.WorkFlowEngineQueryUtil;
import com.wfe.mapping.client.WfAutofireplan;
import com.wfe.mapping.client.WfFirePlanAction;
import com.wfe.mapping.client.WfPlan;

/**
 * @author Kareem.Yehya
 * @since 30 March 2009 <br>
 * 
 * 
 * <br>
 *        This Class Resposible for:- <br>
 * <br>
 *        1- Get non processed actions <br>
 *        2- Select Plan Assosiated with the Action <br>
 *        3- call Project manger to create new project <br>
 * 
 */
public class ActionManger implements Runnable
{

	/**
	 * @author kareem_yehya
	 * @since 30 March 2009 <br>
	 *        This method for load unprocessed actions * @version 1.0.0.1
	 * 
	 * */

	public List<WfFirePlanAction> getWfFirePlanActionList(int maxResults)
	{

		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			List<WfFirePlanAction> retList = WorkFlowEngineQueryUtil.getWfFirePlanActionList(session, maxResults);

			return retList;

		}catch(Exception e)
		{

			e.printStackTrace();
			return null;
		}finally
		{
			EngineUtil.closeSession(session);
		}

	}

	/**
	 * @author kareem_yehya
	 * @since 30 March 2009 <br>
	 *        This method for load plans associated with this actions
	 * 
	 * 
	 * */

	public List<WfAutofireplan> getActionAutoFirePlans(WfFirePlanAction wfFirePlanAction)
	{
		Session session = null;

		try
		{
			session = SessionUtil.getSession();
			List<WfAutofireplan> retList = WorkFlowEngineQueryUtil.getActionAutoFirePlans(session, wfFirePlanAction);
			return retList;
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}finally
		{
			EngineUtil.closeSession(session);

		}
	}

	/**
	 * @author kareem_yehya
	 * @since 30 March 2009 <br>
	 *        This method for load plans associated with this actions
	 * 
	 * 
	 * */

	public WfPlan getWfPlan(WfAutofireplan wfAutofireplan)
	{

		Session session = null;
		try
		{

			session = SessionUtil.getSession();
			WfPlan plan = WorkFlowEngineQueryUtil.getWfPlan(session, wfAutofireplan);

			return plan;

		}catch(Exception e)
		{

			e.printStackTrace();
			return null;
		}finally
		{
			EngineUtil.closeSession(session);

		}

	}

	public void run()
	{

		while(true)
		{
			try
			{
				System.out.println("Enter Action manger main Thread");
				ProjectManger ProjectManger = new ProjectManger();
				List<WfFirePlanAction> nonExecutedActions = getWfFirePlanActionList(100);
				if(nonExecutedActions.size() > 0)
				{
					for(WfFirePlanAction action : nonExecutedActions)
					{
						List<WfAutofireplan> planList = getActionAutoFirePlans(action);

						if(planList != null)
						{
							System.out.println("Plans to be fired  = " + planList.size());
							for(WfAutofireplan fire : planList)
							{
								ProjectManger.executeAction(getWfPlan(fire), action);

							}// for End

						}// if end
					}// action for end
				}// nonExecutedActions if end
				else
				{
					System.out.println("No Any new Coming Actions");
				}

				Thread.sleep(500);

			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}

	}

	public static void main(String args[])
	{
		(new Thread(new ActionManger())).start();
	}

}
